<template>
  <PageWrapper dense contentFullHeight fixed-height>
    <BasicTable @register="registerTable" @row-click="handleClick">
      <template #toolbar>
        <div>
          <a-button type="primary" @click="handleCreate">
            {{ t('新增') }}
          </a-button>
          <a-button @click="handleEnable" :disabled="curRow == -1" class="ml-5px">{{
            curRow == 1 ? t('禁用') : t('启用')
          }}</a-button>
        </div>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              onClick: handleDelete.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <DataDesign
      v-if="dialogvisible"
      @register="registerDesignModal"
      @success="reload"
      @close="handleClose"
    />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction, FormSchema, BasicColumn } from '/@/components/Table';

  import { PageWrapper } from '/@/components/Page';
  import { useMessage } from '/@/hooks/web/useMessage';
  import DataDesign from './components/FuncDesign.vue';
  import { useModal } from '/@/components/Modal';
  import { h, ref, createVNode } from 'vue';

  import { Modal, Tag } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import { useI18n } from '/@/hooks/web/useI18n';

  import { deleteMobileFuncData, getFuncPageList, updateFuncEnableMark } from '/@/api/mobileDesign';

  const { t } = useI18n();
  const dialogvisible = ref(true);
  const curRow = ref(0);
  const curId = ref();
  const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: t('关键字'),
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: t('请输入功能名称'),
      },
    },
    {
      field: 'formType',
      label: t('功能类型'),
      component: 'Select',
      colProps: { span: 8 },
      componentProps: {
        placeholder: t('请选择功能类型'),
        options: [
          { label: '系统页面', value: 0 },
          { label: '自定义页面', value: 1 },
        ],
      },
    },
    {
      field: 'enabledMark',
      label: t('页面状态'),
      component: 'Select',
      colProps: { span: 8 },
      componentProps: {
        placeholder: t('请选择页面状态'),
        options: [
          { label: '启用', value: 1 },
          { label: '禁用', value: 0 },
          { label: '草稿', value: -1 },
        ],
      },
    },
  ];

  const columns: BasicColumn[] = [
    {
      dataIndex: 'name',
      title: t('功能名称'),
      align: 'left',
    },

    {
      dataIndex: 'funcDescribe',
      title: t('功能描述'),
      align: 'left',
    },
    {
      dataIndex: 'formType',
      title: t('功能类型'),
      align: 'left',
      customRender: ({ record }) => {
        const color = record.formType == 1 ? 'blue' : 'yellow';
        const text = record.formType == 1 ? '自定义表单' : '系统表单';
        return h(Tag, { color: color }, () => text);
      },
    },
    {
      dataIndex: 'enabledMark',
      title: t('状态'),
      align: 'left',
      customRender: ({ record }) => {
        const color =
          record.enabledMark == 1 ? 'blue' : record.enabledMark == -1 ? 'yellow' : 'red';
        const text = record.enabledMark == 1 ? '正常' : record.enabledMark == -1 ? '草稿' : '停用';
        return h(Tag, { color: color }, () => text);
      },
    },
    {
      dataIndex: 'menuRemark',
      title: t('备注'),
      width: 120,
    },
  ];
  const { notification } = useMessage();

  const [registerDesignModal, { openModal }] = useModal();
  const [registerTable, { reload, clearSelectedRowKeys, getSelectRows }] = useTable({
    title: t('功能页列表'),
    api: getFuncPageList,
    rowKey: 'id',
    columns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
      fieldMapToTime: [],
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    actionColumn: {
      width: 80,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    rowSelection: { type: 'radio' },
  });

  function handleCreate() {
    openModal(true, {
      title: t('添加功能'),
    });
  }

  function handleEdit(record: Recordable) {
    openModal(true, {
      title: t('编辑功能'),
      isUpdate: true,
      id: record.id,
      appMenuId: record.appMenuId,
      enabledMark: record.enabledMark,
      undoAble: true,
    });
  }

  function handleDelete(record: Recordable = {}) {
    const ids = record.id;

    Modal.confirm({
      title: t('提示'),
      icon: createVNode(ExclamationCircleOutlined),
      content: t('确定要删除所选项吗？'),
      onOk() {
        deleteMobileFuncData(ids).then(() => {
          reload();
          notification.success({
            message: t('提示'),
            description: t('删除成功'),
          });
        });
      },
      onCancel() {},
      okText: t('确认'),
      cancelText: t('取消'),
    });
  }

  async function handleEnable() {
    let rows = getSelectRows();
    if (rows.length > 0) {
      curRow.value = rows[0].enabledMark;
      curId.value = rows[0].id;
      let res = await updateFuncEnableMark({
        id: curId.value,
        enabledMark: curRow.value == 1 ? 0 : 1,
      });
      if (res) {
        notification.success({
          message: t('提示'),
          description: t('修改状态成功'),
        });
        reload();
        curRow.value = 0;
        curId.value = '';
        clearSelectedRowKeys();
      } else {
        notification.error({
          message: t('提示'),
          description: t('修改状态失败'),
        });
      }
    } else {
      notification.warning({
        message: t('提示'),
        description: t('请选择一条数据操作'),
      });
    }
  }
  function handleClick() {
    let rows = getSelectRows();
    if (rows.length > 0) {
      curRow.value = rows[0].enabledMark;
      curId.value = rows[0].id;
    }
  }
  function handleClose() {
    dialogvisible.value = !dialogvisible.value;
    setTimeout(() => {
      dialogvisible.value = !dialogvisible.value;
    }, 100);
  }
</script>
<style lang="less" scoped></style>
